package com.eedi.framework.file.org;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.file.controller.sys.vo.FilePresignedUrlRespVO;
import com.eedi.framework.file.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "经营界面 - 文件存储")
@RestController
@RequestMapping("/file")
@Validated
@Slf4j
public class OrgFileController {

    @Resource
    private FileService fileService;

    @GetMapping("/upload-public/presigned")
    @Operation(summary = "上传文件前，获取的token 针对公开桶的情况", description = "前端在调用上传文件到文件存储服务器前，调用该接口获取真实的上传地址")
    public CommonResult<FilePresignedUrlRespVO> uploadPublicToken(@RequestParam("path") String path) {
        return success(fileService.getFilePresignedUrlForPulbic(path));
    }

    @GetMapping("/upload/presigned")
    @Operation(summary = "上传文件前，获取的token 针对私有桶的情况", description = "前端在调用上传文件到文件存储服务器前，调用该接口获取真实的上传地址")
    public CommonResult<FilePresignedUrlRespVO> uploadToken(@RequestParam("path") String path) {
        return success(fileService.getFilePresignedUrl(path));
    }

    @GetMapping("/download/presigned")
    @Operation(summary = "下载或者渲染文件时，文件真实url，针对私有桶的情况", description = "前端在下载或者渲染私有文件，调用该接口来获取访问Url")
    public CommonResult<String> downloadToken(@RequestParam("path") String path) {
        return success(fileService.getPresignedObjectDownloadUrl(path));
    }

}
